package org.nmgyjt.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.nmgyjt.springboot.beans.admin.StatisticBean;
import org.nmgyjt.springboot.entity.ApiRes;
import org.nmgyjt.springboot.entity.admin.CsSystemAdmin;
import org.nmgyjt.springboot.entity.expert.ExpertInfo;
import org.nmgyjt.springboot.entity.rescueTeam.RescueTeam;
import org.nmgyjt.springboot.mapper.CsSystemAdminMapper;
import org.nmgyjt.springboot.mapper.ExpertMapper;
import org.nmgyjt.springboot.service.ExpertService;
import org.nmgyjt.springboot.utils.ZWWXUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExpertServiceImpl extends ServiceImpl<ExpertMapper, ExpertInfo> implements ExpertService {
    @Autowired
    private ExpertMapper expertMapper;
    @Autowired
    private ExpertService expertService;
    @Autowired
    CsSystemAdminMapper csSystemAdminMapper;
    @Autowired
    ZWWXUtils zwwxUtils;


    @Override
    public ApiRes insertOrUpdate(List<ExpertInfo> expertInfoList) {
        try {
            List<String> ids = new ArrayList<>();
            if (expertInfoList.size() > 0) {
                for (ExpertInfo expertInfo : expertInfoList) {
                    expertInfo.setState("0");
                    if (!ObjectUtils.isEmpty(expertInfo.getId())) {
                        ids.add(expertInfo.getId());
                    }
                }
                //删除前端未传输的装备id数据
                QueryWrapper<ExpertInfo> expertInfoQueryWrapper = new QueryWrapper<>();
                if (ids.size() > 0) {
                    expertInfoQueryWrapper.notIn("id", ids);
                }
                expertMapper.delete(expertInfoQueryWrapper);

                expertService.saveOrUpdateBatch(expertInfoList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ApiRes.customFail("保存失败！");
        }
        return ApiRes.ok();
    }

    @Override
    public ExpertInfo getByUserId(String userId) {
        QueryWrapper<ExpertInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        ExpertInfo expertInfo=expertMapper.selectOne(queryWrapper);
        return expertInfo;
    }

    @Override
    public Map<String, Object> getExpertStatistics(StatisticBean statisticBean) {
        QueryWrapper<CsSystemAdmin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",statisticBean.getCreatedBy());
        CsSystemAdmin csSystemAdmin=csSystemAdminMapper.selectOne(queryWrapper);
        Map<String, Object> retMap = new LinkedHashMap<>();
        if(ObjectUtils.isEmpty(csSystemAdmin)){
            return retMap;
        }
        String sql = " AND r.state <> '1'";
        if (StringUtils.isNotEmpty(statisticBean.getCreatedBy())) {
            sql = sql + " AND a.org_id='" + csSystemAdmin.getOrgId() + "'";
        }else{
            sql = sql + " AND a.org_id='-1'";
        }
        if(StringUtils.isNotEmpty(statisticBean.getName())){
            sql=sql+" AND r.expert_name like '%"+statisticBean.getName()+"%'";
        }
        if(StringUtils.isNotEmpty(statisticBean.getType())){
            sql=sql+" AND ext.source_type='"+statisticBean.getType()+"'";
        }

        List<Map<String,Object>> List=expertMapper.statisticExpert(sql);
        Integer refugeCount=expertMapper.statisticExpertCount(csSystemAdmin.getOrgId());
        Integer refugeTypeCount=expertMapper.statisticExpertTypeCount(csSystemAdmin.getOrgId());
        retMap.put("expertTypeCount",refugeTypeCount);
        retMap.put("expertCount",refugeCount);
        retMap.put("list",List);
        return retMap;
    }

    @Override
    public void snycZwwxByHeadPhone() {
        QueryWrapper<ExpertInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.isNotNull("phone");
        queryWrapper.ne("snyc_state","0");
        List<ExpertInfo> expertInfoList=expertMapper.selectList(queryWrapper);
        for(ExpertInfo expertInfo:expertInfoList){
            String ssoAccessToken=zwwxUtils.getGovWxToken();
            Integer code=zwwxUtils.getZWUserInfo(expertInfo.getPhone(),"expert-detail/index?userId="+expertInfo.getUserId(),ssoAccessToken);
            expertInfo.setSnycState(code.toString());
            this.saveOrUpdate(expertInfo);
        }
    }
}
